﻿using Cube.Data;
using System;
using System.Drawing;

namespace CubeForm
{
    class ProjectPainter
    {
        private Cube.Data.Cube cube;
        private Graphics graphics;
        private int size = 12;
        private int leak = 1;
        public ProjectPainter(Cube.Data.Cube cube, Graphics g)
        {
            this.cube = cube;
            graphics = g;
        }
        public void Draw()
        {
            var startPoint = new Point(0, 0);
            var up = cube.GetFace(Faces.Up);
            var down = cube.GetFace(Faces.Down);
            var left = cube.GetFace(Faces.Left);
            var right = cube.GetFace(Faces.Right);
            var front = cube.GetFace(Faces.Front);
            var behind = cube.GetFace(Faces.Behind);
            DrawFace(up, new Point(startPoint.X + 4 * (size + leak), startPoint.Y));
            DrawFace(left, new Point(startPoint.X, startPoint.Y + 4 * (size + leak)));
            DrawFace(front, new Point(startPoint.X + 4 * (size + leak), startPoint.Y + 4 * (size + leak)));
            DrawFace(right, new Point(startPoint.X + 8 * (size + leak), startPoint.Y + 4 * (size + leak)));
            DrawFace(behind, new Point(startPoint.X + 12 * (size + leak), startPoint.Y + 4 * (size + leak)));
            DrawFace(down, new Point(startPoint.X + 4 * (size + leak), startPoint.Y + 8 * (size + leak)));
        }

        private void DrawFace(CubeColor[,] face, Point s)
        {
            var startX = s.X;
            var startY = s.Y;
            for(var i = 0; i < 3; i++)
            {
                for(var j = 0; j < 3; j++)
                {
                    graphics.FillRectangle(GetBrush(face[i,j]), startX + j * (size + leak), startY + i * (size + leak), size, size);
                }
            }
        }

        private Brush GetBrush(CubeColor color)
        {
            switch (color)
            {
                case CubeColor.Blue:
                    return Brushes.Blue;
                case CubeColor.Green:
                    return Brushes.Green;
                case CubeColor.Orange:
                    return Brushes.Orange;
                case CubeColor.Red:
                    return Brushes.Red;
                case CubeColor.White:
                    return Brushes.Black;
                case CubeColor.Yellow:
                    return Brushes.Yellow;
                default:
                    throw new Exception();
            }
        }
    }
}
